from flask_admin.contrib.sqla import ModelView


class Scheduler:
    def __init__(self, app=None):
        self.app = app
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        

class scheduler_view(ModelView):
    can_delete = True
    can_edit = True
    can_create = True
    column_labels = dict(
        id=u'ID',
        name=u'名称',
        description=u'描述',
        cron=u'cron表达式',
        task=u'任务',
        args=u'参数',
        kwargs=u'关键字参数',
        is_active=u'是否启用',
        creator=u'创建者',
        create_time=u'创建时间',
        update_time=u'更新时间',
    )
    column_exclude_list = (
        'id',
        'creator',
        'create_time',
        'update_time',
    )
    column_searchable_list = (
        'name',
        'description',
        'cron',
        'task',
        'args',
        'kwargs',
        'is_active',
    )
    column_filters = (
        'name',
        'description',
        'cron',
        'task',
        'args',
        'kwargs',
        'is_active',
    )
    column_default_sort = ('id', True)
    column_formatters = dict(
        create_time=lambda v, c, m, p: m.create_time.strftime('%Y-%m-%d %H:%M:%S'),
        update_time=lambda v, c, m, p: m.update_time.strftime('%Y-%m-%d %H:%M:%S'),
    )
    form_excluded_columns = (
        'id',
        'creator',
        'create_time',
        'update_time',
    )
    form_choices = dict(
        is_active=(
            (True, u'是'),
            (False, u'否'),
        ),
    )
    form_widget_args = dict(
        name=dict(
            placeholder=u'请输入名称',
            description=u'请输入名称',
        ),
        description=dict(
            placeholder=u'请输入描述',
            description=u'请输入描述',
        ),
        cron=dict(
            placeholder=u'请输入cron表达式',
            description=u'请输入cron表达式',
        ),
        task=dict(
            placeholder=u'请输入任务',
            description=u'请输入任务',
        ),
        args=dict(
            placeholder=u'请输入参数',
            description=u'请输入参数',
        ),
        kwargs=dict(
            placeholder=u'请输入关键字参数',
            description=u'请输入关键字参数',
        ),
        is_active=dict(
            placeholder=u'请选择是否启用',
            description=u'请选择是否启用',
        ),
    )
    form_rules = (
        'name',
        'description',
        'cron',
        'task',
        'args',
        'kwargs',
        'is_active',
    )
    form_overrides = dict(
        args=lambda: TextAreaField,
        kwargs=lambda: TextAreaField,
    )
    form_ajax_refs = dict(
        task=dict(
            fields=('name', 'description'),
            page_size=10,
        ),
    )
